<?php

namespace app\admin\controller;
use think\Db;

class Notice extends Common{

	public function index(){
		return $this->fetch();
	}
	/*
	数据接口
	*/
	public function dataList(){
		$limit = request()->param('limit')?:20;
		$data  = Db::table('tp_notice')
				->order('id desc')
				->paginate($limit);
		$data = $data->toArray();
		return $this->tableData($data);
	}
	/*
	删除公告
	*/
	public function del(){
		$id = request()->param('id');
		$res = Db::table('tp_notice')->where('id',$id)->delete();
		if ($res) {
			return ['code'=>0,'msg'=>'successs'];
		}
	}
	/*
	编辑公告
	 */
	public function editNotice(){
		if (request()->method() == 'POST') {
			$data = [];
			$id = request()->param('id');
			$data['title']   = request()->param('title');
			$data['content'] = input('post.content','',null);
			if ($id) {
				$res  = Db::table('tp_notice')->where('id',$id)->update($data);
			} else {
				$res  = Db::table('tp_notice')->insert($data);
			}
			
			if ($res) {
				return ['code'=>0,'msg'=>'success'];
			} else {
				return ['code'=>1,'msg'=>'新增失败'];
			}		
		} else {
			$id = request()->param('id');
			$data = [];
			if ($id) {
				$data  = Db::table('tp_notice')->where('id',$id)->find(); 
			}
			$this->assign('data',$data);
			return $this->fetch();
		}
	}
}